﻿using System;
using System.Collections.Generic;
using System.Text;
using T.ServiceComponent.Session.Service;
using AppContext = T.ServiceComponent.Session.Service.AppContext;

namespace T.FAS.Runtime.Base.UnitTests
{
    public class SessionHelper
    {
        private static string sessionId = null;

        /// <summary>
        /// 设置当前登录用户(生产代码禁用此方法，主要用于单元测试)
        /// </summary>
        /// <param name="user">登录用户</param>
        public static void SetCurrentUser(UserData user)
        {
            if (AppContext.Current.Session == null)
            {
                sessionId = Guid.NewGuid().ToString();
                AppContext.Current.SessionID = sessionId;
            }
            AppContext.Current.Session.SetSession<UserData>(user);
        }

        /// <summary>
        /// 获取客户内码
        /// </summary>
        /// <returns></returns>
        public static UserDataEX CurrentUser
        {
            get
            {
                try
                {
                    SessionState session = AppContext.Current.Session;
                    if (session != null)
                    {
                        var user = session.GetSession<UserData>();
                        if (user != null)
                        {
                            UserDataEX dataEX = new UserDataEX()
                            {
                                UserId = user.UserId,
                                Account = user.Account,
                                Mobile = user.Mobile,
                                UserName=user.UserName,
                                Alias=user.Alias
                            };
                            if (user.PersonalData != null)
                            {
                                dataEX.CustomerID = user.PersonalData.CustomerID;
                                dataEX.SPDeviceID = user.PersonalData.SPDeviceID;
                                dataEX.OpenId = user.PersonalData.OpenId;
                            }
                            return dataEX;
                        }
                        return null;
                    }
                }
                catch { }
                return null;
            }
        }
    }
}
